package com.iris.live.services.common.reconsitution;

import com.google.common.collect.Maps;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/29.
 */
public class XmlAnalysisUtil {

    /**
     * 返回映射配置集合列表
     * @param filePath
     * @return
     */
    public static List<Map<String,Object>> analysisXmlList(String filePath,String nodeName){
        Map<String,Object> map = analysisXml(filePath,nodeName);
        return (List<Map<String,Object>>)map.get("list");
    }

    /**
     * 返回映射配置中的用户EXCEL标题
     * @param filePath
     * @return
     */
    public static List<String> analysisXmlTitleList(String filePath,String nodeName){
        Map<String,Object> map = analysisXml(filePath,nodeName);
        return (List<String>)map.get("titleList");
    }


    /**
     * 解析本体返回数据
     * @param filePath
     * @return
     * @throws Exception
     */
    private static Map<String,Object> analysisXml(String filePath,String nodeName){
        Map<String,Object> retuMap = Maps.newHashMap();
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        List<String> titleList = new ArrayList<String>();
        try {
            SAXReader reader = new SAXReader();
            // 通过reader对象的read方法加载xml文件,获取docuemnt对象。
            Document document = reader.read(XmlAnalysisUtil.class.getClassLoader().getResourceAsStream(filePath));
            // 通过document对象获取根节点
            Element model = document.getRootElement();
            //指定节点
            Element childModel = model.element(nodeName);
            // 通过element对象的elementIterator方法获取迭代器
            Iterator it = childModel.elementIterator();
            // 遍历迭代器，获取根节点中的信息（书籍）
            while (it.hasNext()) {
                Element book = (Element) it.next();
                Map<String,Object> map = Maps.newHashMap();
                // 获取属性名以及 属性值
                List<Attribute> bookAttrs = book.attributes();
                for (Attribute attr : bookAttrs) {
                    map.put(attr.getName(),attr.getValue());
                    if(ExcelAnalysisUtil.XML_EXCEL_NAME.equals(attr.getName())){
                        titleList.add(attr.getValue());
                    }
                }
                list.add(map);
            /*  //解析子节点的信息
             Iterator itt = book.elementIterator();
              while (itt.hasNext()) {
                   Element bookChild = (Element) itt.next();
                   System.out.println("节点名：" + bookChild.getName() + "--节点值：" + bookChild.getStringValue());
            }*/
                retuMap.put("list",list);
                retuMap.put("titleList",titleList);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return retuMap;
    }


    public static void main(String[] args) {
        List<String> a = new ArrayList<String>();
        a.add("1");
        a.add("2");
        for (String temp : a) {
            if("2".equals(temp))
            {
                a.remove(temp);
            }
        }
        System.out.println(a.toString());

    }
}
